package com.zhang.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhang.pojo.SupplierBean;
import com.zhang.service.ISupplierService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "供应商控制接口")
@RestController
@RequestMapping("supplier")
public class SupplierController {

    @Autowired
    private ISupplierService supplierService; //供应商业务层

    @ApiOperation("查询所有的供应商")
    @GetMapping("findAll")
    public List<SupplierBean> findAll(){
        return supplierService.findAll();
    }


    @ApiOperation("根据供应商和经营范围动态条件查询并分页")
    @GetMapping("findByPageAndItem/{current}/{pageSize}/{name}/{scope}")
    public IPage<SupplierBean> findByPageAndItem(@PathVariable("current")int current,
                                                 @PathVariable("pageSize")int pageSize,
                                                 @PathVariable("name")String name,
                                                 @PathVariable("scope")String scope){
        if(!name.equals("") &&"1".equals(name) ){
            name = null;
        }
        if(!scope.equals("") &&"1".equals(scope)){
            scope = null;
        }
        return supplierService.findByPageAndItem(new Page<>(current,pageSize), name, scope);
    }

    @ApiOperation("添加供应商")
    @PostMapping("add")
    public String add(SupplierBean supplierBean){
        try {
            supplierService.add(supplierBean);
            return "ok";
        } catch (Exception e) {
            return "no";
        }
    }


    @ApiOperation("修改供应商负责人及其联系方式")
    @PutMapping("update")
    public String update(SupplierBean supplierBean){
        try {
            supplierService.update(supplierBean);
            return "ok";
        } catch (Exception e) {
            return "no";
        }
    }

    @ApiOperation("根据id查询供应商")
    @GetMapping("findById")
    public SupplierBean findById(int id){
        return supplierService.findById(id);
    }
}
